from sqlalchemy import Column, Integer, String, JSON, TEXT, func
from sqlalchemy.orm import declarative_base, sessionmaker

from api_utils.wechat_api import WeChatAPI
from database.mysql_utils import MysqlUtils

Base = declarative_base()


class Department(Base):
    __tablename__ = 'department'
    department_id = Column(Integer, primary_key=True)
    name = Column(String(255))
    name_en = Column(String(255))
    department_leader = Column(JSON)
    parentid = Column(Integer)
    order = Column(Integer)


class WechatUsers(Base):
    __tablename__ = 'wechat_users'

    # 定义表的列
    id = Column(Integer, primary_key=True)
    userid = Column(String(255))
    name = Column(String(255))
    department = Column(JSON)
    open_userid = Column(String(255))


class WechatGroup(Base):
    __tablename__ = 'wechat_groups'
    chatid = Column(String(255), primary_key=True)
    name = Column(String(50))
    owner = Column(String(50))
    userlist = Column(JSON)


class Message(Base):
    __tablename__ = 'message'
    id = Column(Integer, primary_key=True)
    chatid = Column(String(255), primary_key=True)
    msg = Column(JSON)


Base.metadata.create_all(MysqlUtils.mysql_connect())
Session = sessionmaker(bind=MysqlUtils.mysql_connect())


class WechatData:

    @staticmethod
    def insert_wechat_department():
        session = Session()
        wechat_api = WeChatAPI()
        department_list = wechat_api.get_department_list()
        print(department_list)
        for department_info in department_list:
            department_id = department_info.get('id')
            name = department_info.get('name')
            parent_id = department_info.get('parentid')
            order = department_info.get('order')
            department_leader = department_info.get('department_leader')
            department = Department(department_id=department_id, name=name, parentid=parent_id, order=order,
                                    department_leader=department_leader)
            session.add(department)
            session.commit()
        session.close()

    @staticmethod
    def get_department_id_by_name():
        session = Session()
        name = "蓝创商务"
        department_id = session.query(Department.department_id).filter(Department.name == name).first()
        print(department_id[0])
        return department_id[0]

    @staticmethod
    def get_wechat_users():
        department_id = WechatData.get_department_id_by_name()
        wechat_api = WeChatAPI()
        wechat_user_list = wechat_api.get_wechat_user_list(department_id)
        print(wechat_user_list)
        session = Session()
        for wechat_user in wechat_user_list:
            userid = wechat_user.get('userid')
            name = wechat_user.get('name')
            department = wechat_user.get('department')[0]
            wechat_user = WechatUsers(userid=userid, name=name, department=department)
            session.add(wechat_user)
        session.commit()
        session.close()

    @staticmethod
    def create_wechat_group(group_name, member_list):
        session = Session()
        wechat_api = WeChatAPI()
        userid_list = []
        for name in member_list:
            ret = session.query(WechatUsers).filter(WechatUsers.name == name).first()
            session.commit()
            print(ret.userid)
            userid_list.append(ret.userid)
        session.close()
        print(userid_list[0], userid_list)
        owner = userid_list[0]
        wechat_group_id = wechat_api.create_wechat_group(group_name, owner, userid_list)
        return wechat_group_id, owner, userid_list

    @staticmethod
    def insert_wechat_group_info(wechat_group_id, group_name, owner, userlist):
        session = Session()
        wechart_group = WechatGroup(chatid=wechat_group_id, name=group_name, owner=owner, userlist=userlist)
        session.add(wechart_group)
        session.commit()
        session.close()

    @staticmethod
    def send_wechat_text_message(wechat_group_id, message):
        wechat_api = WeChatAPI()
        wechat_api.send_wechat_message(wechat_group_id, message)

    @staticmethod
    def send_file_to_wechat_group(chatid, file_path):
        wechat_api = WeChatAPI()
        wechat_api.send_file_to_group(chatid, file_path)
